#include "gamepersent.h"
#include "utils/deviceutils.h"
#include "QThread"
#include "QDateTime"
#include "utils/runshopgoodsmanager.h"
#include "QDir"

/**
 * 绑定游戏
 * @brief Win32Util::bindGame
 * @param className
 * @return
 */
void GamePersent::test()
{
    qDebug() << "GamePersent==" << QThread::currentThreadId();
}

GamePersent::GamePersent(QWidget *widget):BasePersent(widget)
{
    this->widget = widget;
}

void GamePersent::bindGame(QString className)
{
    QList<DWORD> pid = Win32Util::findPidProcessName(TLBB_PROCESS_NAME);
    if(pid.isEmpty()){
        DeviceUtils::sendLog(widget, "绑定游戏失败,请检查游戏是否开启");
        return;
    }
    GameManager::getInstantce()->hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid.at(0));
    GameManager::getInstantce()->baseAddress = Win32Util::getBaseAddress(TLBB_PROCESS_NAME, BASE_ADDRESS);
    if(GameManager::getInstantce()->baseAddress == 0){
        DeviceUtils::sendLog(widget, "绑定游戏失败,请检查游戏是否开启");
        return;
    }

    GameManager::getInstantce()->wHandle = FindWindowW((LPCWSTR)className.unicode(), NULL);
    if( GameManager::getInstantce()->wHandle == nullptr){

        DeviceUtils::sendLog(widget, "绑定游戏失败,请检查游戏是否开启");
        return;
    }
    if(IsIconic ( GameManager::getInstantce()->wHandle)){
        DeviceUtils::sendLog(widget, "绑定游戏失败,请检查游戏是否处于非最小化状态");
        return;
    }
    QRect rect = Win32Util::getGameRect();
    unsigned int width = rect.right() - rect.left();
    unsigned int height = rect.bottom() - rect.top();
    if(width < 816 || height < 740){
        DeviceUtils::sendLog(widget, QString("绑定游戏失败,游戏窗口宽高不得小于816x740,当前游戏窗口宽高%1x%2").arg(width).arg(height));
        return;
    }
    WINBOOL result = Win32Util::setSetWindowPos();
    if(result){
        isBindGame = true;
        GameManager::getInstantce()->gameRect = Win32Util::getGameRect();
        QPoint point = getRolesPoint();
        DeviceUtils::sendLog(widget, QString("绑定游戏成功,角色信息：地图%1，坐标为: %2,%3，血量:%4,蓝量:%5").arg(getMapString()).arg(point.x()).arg(point.y()).arg(getBlood()).arg(getMagic()));
        ShowWindow(GameManager::getInstantce()->wHandle,SW_SHOW);

    }else{
        DeviceUtils::sendLog(widget, "绑定游戏失败,未知原因");
    }
}

void GamePersent::closeOtherRole()
{
    Sleep(200);
    Win32Util::keyboardClick(VK_F11);
    Sleep(200);
    Win32Util::keyboardClick(VK_F12);
}

bool GamePersent::checkOpenAutoWay()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/zdxl.png", Scalar(18, 134, 102), Scalar(29, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.6f,0.2f, 1, 0.7f));
}

void GamePersent::moveRoles(CityNode *cityNode, CallBack *callback , bool isWait)
{
    if(!checkOpenAutoWay()){ //判断自动寻路面板是否打开 true 打开 false 未打开
        qDebug() << "自动寻路面板未打开";
        Win32Util::keyboardClick(VK_MENU,VK_OEM_3); //VK_OEM_3为~键SleepQRectF(0.6f, 0.3f, 1, 0.9)
    }
    if(cityNode->cityImg.isNull() || cityNode->cityImg.isEmpty()){
        moveByPoint(cityNode->x, cityNode->y, callback, isWait);
    }else{
        moveByImg(cityNode,callback, isWait);
    }

}

void GamePersent::moveRoles(int x, int y, CallBack *callback , bool isWait)
{
    if(!checkOpenAutoWay()){ //判断自动寻路面板是否打开 true 打开 false 未打开
        qDebug() << "自动寻路面板未打开";
        Win32Util::keyboardClick(VK_MENU,VK_OEM_3); //VK_OEM_3为~键SleepQRectF(0.6f, 0.3f, 1, 0.9)
    }
    moveByPoint(x, y, callback, isWait);
}

bool GamePersent::clickDest(QString img, Scalar startScalar, Scalar endScalar, float threshold, QRectF percentRectf, bool isDoubleClick)
{
    if(img.isNull() || img.isEmpty() || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
    QRectF outRectF;
    if(threshold == 0 ){
        threshold = Setting::getInstance()->getImgDiscern();
    }
    if(!ImgHelper::getGameMatchPoint(img, startScalar, endScalar, threshold, percentRectf, &outRectF)){
        return false;
    }
    Win32Util::moveMouse((outRectF.left() + outRectF.right())/2, (outRectF.top() + outRectF.bottom())/2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
    Win32Util::mouseLeftClick(isDoubleClick);
}

void GamePersent::moveByPoint(int x, int y, CallBack *callback, bool isWait){
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    //查找X坐标输入框位值
    if(callback->isCancelTask())return;
    ImgHelper::getGameMatchPoint(":/runshop/icon/input.png",textStartScalar ,textEndScalar, Setting::getInstance()->getImgDiscern(), QRectF(0.6f, 0.3f, 1, 0.9f), &inputRectF);
    inputX(x);
    inputY(y);
    if(callback != nullptr && callback->isCancelTask())return;
    //移动鼠标到"移动"按钮

    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset + 1500));
    while(ImgHelper::getGameMatchPoint(":/runshop/icon/inputX.png",textStartScalar ,textEndScalar, Setting::getInstance()->getImgDiscern(), QRectF(0.6f, 0.3f, 1, 0.9f))){
        inputX(x);
        Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(callback->isCancelTask())return;
    }
    if(callback->isCancelTask())return;
    while(ImgHelper::getGameMatchPoint(":/runshop/icon/inputY.png",textStartScalar ,textEndScalar,Setting::getInstance()->getImgDiscern(), QRectF(0.6f, 0.3f, 1, 0.9f))){
        inputY(y);
        Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(callback != nullptr && callback->isCancelTask())return;
    }
    addSpeed();
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(callback->isCancelTask())return;
    Win32Util::moveMouse( (inputRectF.left() + 7 * inputRectF.right()) / 8, (inputRectF.top() + inputRectF.bottom()) /2);
    //点击“移动”按钮
    if(callback->isCancelTask())return;
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::mouseLeftClick();
    if(callback->isCancelTask())return;
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::keyboardClick(VK_ESCAPE);
    GameManager::getInstantce()->isMove = true;
    GameManager::getInstantce()->isNeedCheckMove = true;
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QPoint point = getRolesPoint();
    while((x != point.x() || y != point.y()) && isWait){
        point = getRolesPoint();
        QThread::msleep(100);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) break;
    }
    if((x == point.x() && y == point.y()) || isWait){
        GameManager::getInstantce()->isNeedCheckMove = false;
        checkCityNode = nullptr;
    }

}

void GamePersent::moveByImg(CityNode * cityNode, CallBack *callback, bool isWait)
{
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(callback->isCancelTask())return;
    clickDest(cityNode->cityImg, textStartScalar ,textEndScalar, Setting::getInstance()->getImgDiscern(),QRectF(0.6f, 0.3f, 1, 0.9f), true);
    if(callback->isCancelTask())return;
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::keyboardClick(VK_ESCAPE);
    GameManager::getInstantce()->isMove = true;
    GameManager::getInstantce()->isNeedCheckMove = true;
    QPoint point = getRolesPoint();
    qDebug() << "x,y= "<<cityNode->x << "," <<cityNode->y << ",curentPoint" << point.x() <<"," <<point.y() ;
    while((qAbs(cityNode->x - point.x()) > 4 || qAbs(cityNode->y - point.y()) > 4) && isWait){
        point = getRolesPoint();
        QThread::msleep(100);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) break;
    }
    if((qAbs(cityNode->x - point.x()) < 4 && qAbs(cityNode->y - point.y()) < 4) || isWait){
        GameManager::getInstantce()->isNeedCheckMove = false;
        checkCityNode = nullptr;
    }
}

void GamePersent::changeRole(int roleIndex)
{
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::keyboardClick(VK_ESCAPE);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/back_login.png", Scalar(25, 75, 130), Scalar(27, 139, 255), 0.7, QRectF(0.2f,0.2f,0.8f,0.8f), qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug()<< "未找到返回登录界面按钮";
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::moveMouse((qRectF->left() + qRectF->right()) / 2, (qRectF->top() + qRectF->bottom()) / 2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::mouseLeftClick();
    while(!getMapString().contains("tlbb_login")){
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    }
    QThread::msleep(3000);
    Win32Util::setSetWindowPos();
    QThread::msleep(2500);
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/sure_select.png", Scalar(18, 109, 208), Scalar(26, 134, 255), 0.7, QRectF(0.2f,0.5f,0.9f,0.8f), qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug()<< "未找到确认选择按钮";
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::moveMouse((qRectF->left() + qRectF->right()) / 2, (qRectF->top() + qRectF->bottom()) / 2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::mouseLeftClick();
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset) + 500);
    //输入游戏帐号
    Win32Util::keyboardClick(GameManager::getInstantce()->currentRole->account, false);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::keyboardClick(VK_TAB);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    //输入游戏密码
    Win32Util::keyboardClick(GameManager::getInstantce()->currentRole->password, false);
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/startGame.png", Scalar(14, 243, 71), Scalar(20, 255, 130), 0.7, QRectF(0.2f,0.2f,0.8f,0.8f), qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug()<< "未找到确认选择按钮";
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::moveMouse((qRectF->left() + qRectF->right()) / 2, (qRectF->top() + qRectF->bottom()) / 2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::mouseLeftClick();
    while(!getMapString().contains("xuanren")){
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset) + 500);
    Win32Util::setSetWindowPos();
    qDebug() << "切换第" << roleIndex << "角色";
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset) + 500);
    QList<QSharedPointer<QRectF>> rectfs;
    if(!ImgHelper::getGameMatchMultiPoint(":/runshop/icon/select_role.png", Scalar(60, 253, 253), Scalar(62, 255, 255), 0.7f, &rectfs, QRectF(0.7f,0.1f,0.95f,0.7f)) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug()<< "未找到进入游戏按钮";
        return;
    }

    if(roleIndex <= 0 || (roleIndex > rectfs.size())){
        DeviceUtils::sendLog(widget, "角色索引设置有误，角色索引不应超过当前帐号创建的角色数量");
        return;
    }
    QSharedPointer<QRectF> rolePoint = rectfs.at(roleIndex - 1);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::moveMouse((rolePoint->left() + rolePoint->right()) / 2, (rolePoint->top() + rolePoint->bottom()) / 2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::mouseLeftClick();
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset) + 1000);
    if(!ImgHelper::getGameMatchPoint(":/runshop/icon/goGame.png", Scalar(16, 95,155), Scalar(26, 145, 255), 0.7f, QRectF(0.7f,0.6f,0.95f,0.9f), qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug()<< "未找到进入游戏按钮";
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::moveMouse((qRectF->left() + qRectF->right()) / 2, (qRectF->top() + qRectF->bottom()) / 2);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::mouseLeftClick();
    while(getMapString().contains("xuanren")){
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    }
    QThread::msleep(6000);
    Win32Util::setSetWindowPos();
}

/**
 * @brief GamePersent::goGroupCity
 * @param cityEn
 * @param xiaoWeiLocationX 为0则表示没有校尉
 * @param xiaoWeiLocationY 为0则表示没有校尉
 * @param callback
 * @return
 */
bool GamePersent::goGroupCity(QString cityEn,QString xiaoWeiLocation, CallBack *callback)
{
    QString currentCity =  getMapString();
    if(currentCity.contains(PCITY_LEVEL)){
        return false;
    }
    QList<CityNode *> cityNode;
    if(cityEn != currentCity){
        if(!CityManager::getInstance()->getRunShop(currentCity,cityEn, &cityNode)){
            return false;
        }
    }
    if(!xiaoWeiLocation.isEmpty() && xiaoWeiLocation != "无"){
        CityXiaoWeiBean* xiaoWeiBean = CityManager::getInstance()->getXiaoWeiBean(cityEn, xiaoWeiLocation);
        if(xiaoWeiBean == nullptr){
            return false;
        }
        CityNode *xiaowei = new CityNode("校尉", XIAO_WEI, RunShopGoodsManager::getInstance()->getXiaoWeiLocationsImgs(xiaoWeiLocation), xiaoWeiBean->x, xiaoWeiBean->y);
        cityNode.append(xiaowei);
    }
    QRectF qRectF;
    for(int i = 0 ; i < cityNode.size() ; i++){
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
            return false;
        }
        addBlood();
        QThread::msleep(TimeManager::getInstance()->getDelay());
        addMagic();
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        CityNode *nextCity = cityNode.at(i);
        checkCityNode = nextCity;
        qDebug() <<"准备前往"<< nextCity->name;
        DeviceUtils::sendLog(widget, QString("准备前往%1 %2,%3").arg(nextCity->name).arg(nextCity->x).arg(nextCity->y));
        moveRoles(nextCity, callback);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        if(ImgHelper::getGameMatchPoint(":/runshop/icon/button_sure.png",Scalar(22, 76, 90), Scalar(26, 159, 255),0.7f, QRectF(0.2f, 0.2f, 0.8f, 0.8f), &qRectF)){
            DeviceUtils::sendLog(widget, "点击确定按钮");
            Win32Util::moveMouse((qRectF.left() + qRectF.right())/2, (qRectF.top() + qRectF.bottom())/2);
            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
            Win32Util::mouseLeftClick();
        }

        while(!getMapString().contains(nextCity->enName) && nextCity->enName != XIAO_WEI ){
            QThread::msleep(100);
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        }

        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->scene));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
    }
    return true;

}

bool GamePersent::inputX(int x)
{
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    //移动鼠标到x坐标输入框
    Win32Util::moveMouse((5 * inputRectF.left() + 3 * inputRectF.right()) / 8, (inputRectF.top() + inputRectF.bottom()) /2);
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));

    //点击鼠标左键
    Win32Util::mouseLeftClick();
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));

    Win32Util::keyboardClick(QString::number(x),false);

}

bool GamePersent::inputY(int y)
{
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    //移动鼠标到y轴
    Win32Util::moveMouse( (3 * inputRectF.left() + 5 * inputRectF.right()) / 8, (inputRectF.top() + inputRectF.bottom()) /2);
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    Win32Util::mouseLeftClick();
    Sleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    //输入y坐标

    Win32Util::keyboardClick(QString::number(y), false);

}

bool GamePersent::checkBuy()
{
    return checkYinPiao() || checkYininPiaoFill() || checkPackageFill() || checkYinPiaoHave();
}

bool GamePersent::checkYinPiaoHave()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/yin_piao_not.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f));
}

bool GamePersent::checkHasGoods(QString goodsImg, Scalar startScalar, Scalar endScalar)
{
    if(ImgHelper::getGameMatchPoint(goodsImg,  startScalar, endScalar, Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f))){
        DeviceUtils::sendLog(widget, "商品已售空，15秒后自动刷新");
        QThread::msleep(15000);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        return false;
    }
    return true;
}

bool GamePersent::checkYinPiao()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/check_buy.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f));
}

bool GamePersent::checkYininPiaoFill()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/yinpiao_fill.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f));
}

bool GamePersent::checkPackageFill()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/runshop/package_fill.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f))
            ||ImgHelper::getGameMatchPoint(":/runshop/icon/package_fill.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f))
            ||ImgHelper::getGameMatchPoint(":/runshop/icon/runshop/package_fill2.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f));
}

bool GamePersent::checkOperationFast()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/operation_fast.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f));

}

bool GamePersent::checkContentChange()
{
    return ImgHelper::getGameMatchPoint(":/runshop/icon/content_change.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), Setting::getInstance()->getImgDiscern(), QRectF(0.3,0,0.7,0.4f));
}

bool GamePersent::killNPCs(CallBack *callback, QList<QPoint *> *points, int total)
{
    for(QPoint *point: *points){
        moveRoles(point->x(), point->y(),callback);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        while(GameManager::getInstantce()->isMove){
            QThread::msleep(100);
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        }
        killNPC(total, false);
    }
    DeviceUtils::sendLog(widget, "结束清怪");
}

bool GamePersent::killNPC(int nodeTotal, bool isOnlyOne)
{
    QString skill = Setting::getInstance()->getSkill(); //无CD技能
    QString skill2 = Setting::getInstance()->getSkill2();
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
    int killNPCCount = 0;
    while(true){
        int flag = 0;
        Win32Util::keyboardClick(VK_CONTROL,VK_TAB);
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        if(!ImgHelper::getGameMatchPointAsb(":/kuozhang/icon/kuozhang/npc.png",  Scalar(38, 222, 109),Scalar(60, 255, 139), 0.9f, QRect(270,40,165,45))){
            if(killNPCCount > nodeTotal || nodeTotal == 0){
                DeviceUtils::sendLog(widget, "此节点怪物扫荡完成");
                return true;
            }
        }
        QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
        while(true){
            clickFuncKey(skill);
            if(flag % 5 == 0){
                clickFuncKey(skill2);
                DeviceUtils::sendLog(widget, QString("按下%2键").arg(skill2));
                flag =0;
            }

            QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
            if(ImgHelper::getGameMatchPoint(":/kuozhang/icon/kuozhang/invalid_npc.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), 0.9f, QRectF(0.3,0,0.7,0.4f))
                    ||ImgHelper::getGameMatchPoint(":/kuozhang/icon/kuozhang/please_select_npc.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), 0.9f, QRectF(0.3,0,0.7,0.4f))){
                killNPCCount++;
                if(isOnlyOne){
                    DeviceUtils::sendLog(widget, "击杀怪物");
                    return true;
                }else{

                    if(!ImgHelper::getGameMatchPointAsb(":/kuozhang/icon/kuozhang/npc.png",  Scalar(38, 222, 109),Scalar(60, 255, 139), 0.9f, QRect(270,40,165,45))){
                        qDebug() << "没有npc被选中";
                        break;
                    }
                }
            }
            QThread::msleep(3000);
            if(ImgHelper::getGameMatchPoint(":/kuozhang/icon/kuozhang/dont_kill.png",  Scalar(89, 254, 254),Scalar(91, 255, 255), 0.9f, QRectF(0.3,0,0.7,0.4f))){
                DeviceUtils::sendLog(widget, "不能攻击此目标");
                break;
            }

            if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;
            addBlood();
            QThread::msleep(3000);
            addMagic();
            flag++;
        }
    }
    QThread::msleep(3000);
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return false;

}


void GamePersent::clickNPC(QString img, bool isHide)
{
    QThread::msleep(TimeManager::getInstance()->getDelay());
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(isHide){
        closeOtherRole();
    }

    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QRectF qRectF;
    if(!ImgHelper::getGameMatchPoint(img, Scalar(29, 209, 161), Scalar(31, 255, 255), 0.6f, QRectF(0.2f,0.2,0.9f,0.9f), &qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug() << "未匹配到NPC";
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay());
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, qRectF.bottom() + 100);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击NPC
    DeviceUtils::sendLog(widget, "点击NPC");
}

void GamePersent::clickNPC(QString img, Scalar startScalar, Scalar endScalar, bool isHide)
{
    QThread::msleep(TimeManager::getInstance()->getDelay());
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(isHide){
        closeOtherRole();
    }

    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QRectF qRectF;
    if(!ImgHelper::getGameMatchPoint(img, startScalar, endScalar, 0.6f, QRectF(0.2f,0.2,0.9f,0.9f), &qRectF) || GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL){
        qDebug() << "未匹配到NPC";
        return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay());
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::moveMouse((qRectF.left() + qRectF.right()) / 2, qRectF.bottom() + 100);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击NPC
    DeviceUtils::sendLog(widget, "点击NPC");
}

//死亡或在地府的处理
bool GamePersent::resurgence()
{
    if(GameManager::getInstantce()->roleLocation == ROLE_LOCATION_HELL){
        outHell();
    }else if(GameManager::getInstantce()->roleLocation == ROLE_LOCATION_REND){
        goHell();
    }
}

bool GamePersent::clickFuncKey(QString key)
{
    if(key == "F1"){
        Win32Util::keyboardClick(VK_F1);
    }else if(key == "F2"){
        Win32Util::keyboardClick(VK_F2);
    }else if(key == "F3"){
        Win32Util::keyboardClick(VK_F3);
    }else if(key == "F4"){
        Win32Util::keyboardClick(VK_F4);
    }else if(key == "F5"){
        Win32Util::keyboardClick(VK_F5);
    }else if(key == "F6"){
        Win32Util::keyboardClick(VK_F6);
    }else if(key == "F7"){
        Win32Util::keyboardClick(VK_F7);
    }else if(key == "F8"){
        Win32Util::keyboardClick(VK_F8);
    }else if(key == "F9"){
        Win32Util::keyboardClick(VK_F9);
    }else if(key == "F10"){
        Win32Util::keyboardClick(VK_F10);
    }else{
        return false;
    }
    return true;
}
/**
 * 被杀，灵魂出窍了
 * @brief GamePersent::goHell
 */
void GamePersent::goHell(){
    QTimer::singleShot(TimeManager::getInstance()->getDelay(1500), this, [=](){
        if(ImgHelper::getGameMatchPoint(":/runshop/icon/chuqiao.jpg", qRectF)){
            QTimer::singleShot(TimeManager::getInstance()->getDelay(1500), this, [=](){
                Win32Util::moveMouse(qRectF->right() - 10, (qRectF->top() + qRectF->bottom())/2 );
                QTimer::singleShot(TimeManager::getInstance()->getDelay(1500), this, [=](){
                    Win32Util::mouseLeftClick();
                    QTimer::singleShot(TimeManager::getInstance()->getDelay(5000), this, [=](){
                        outHell();
                    });
                });
            });
        }
    });
}

bool GamePersent::outHell()
{
    QTimer::singleShot(TimeManager::getInstance()->getDelay(1500), this, [=](){

        //先判断自动寻路面板有没打开
        if(checkOpenAutoWay()){
            if(ImgHelper::getGameMatchPoint(":/runshop/icon/hell_meng_po.jpg", qRectF)){
                Win32Util::moveMouse((qRectF->left() + qRectF->right())/2, (qRectF->top() + qRectF->bottom())/2 );
                QTimer::singleShot(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset), this, [=](){
                    Win32Util::mouseLeftClick();
                    if(ImgHelper::getGameMatchPoint(":/runshop/icon/input.png", qRectF)){
                        QTimer::singleShot(TimeManager::getInstance()->getDelay(800), this, [=](){
                            Win32Util::moveMouse(qRectF->right() - 10, (qRectF->top() + qRectF->bottom())/2 );
                            QTimer::singleShot(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset), this, [=](){
                                Win32Util::mouseLeftClick();
                                QTimer::singleShot(TimeManager::getInstance()->getDelay(800), this, [=](){
                                    selectHell();
                                });

                            });
                        });
                    }
                });
            }

        }else{
            if(ImgHelper::getGameMatchPoint(":/runshop/icon/auto_way.png", qRectF)){
                Win32Util::moveMouse((qRectF->left() + qRectF->right())/2, (qRectF->top() + qRectF->bottom())/2 );
                QTimer::singleShot(TimeManager::getInstance()->getDelay(), this, [=](){
                    Win32Util::mouseLeftClick(); //打开自动面板
                    QTimer::singleShot(TimeManager::getInstance()->getDelay(500), this, [=](){
                        if(ImgHelper::getGameMatchPoint(":/runshop/icon/hell_meng_po.jpg", qRectF)){
                            Win32Util::moveMouse((qRectF->left() + qRectF->right())/2, (qRectF->top() + qRectF->bottom())/2 );
                            QTimer::singleShot(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset), this, [=](){
                                Win32Util::mouseLeftClick();
                                if(ImgHelper::getGameMatchPoint(":/runshop/icon/input.png", qRectF)){
                                    QTimer::singleShot(TimeManager::getInstance()->getDelay(800), this, [=](){
                                        Win32Util::moveMouse(qRectF->right() - 10, (qRectF->top() + qRectF->bottom())/2 );
                                        QTimer::singleShot(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset), this, [=](){
                                            DeviceUtils::sendLog(widget, "出地府中...");
                                            QTimer::singleShot(TimeManager::getInstance()->getDelay(5000), this, [=](){
                                                selectHell();
                                            });
                                        });
                                    });
                                }
                            });
                        }

                    });
                });
            }else{
                qDebug() << "获取地府自动寻路失败";
            }
        }
    });
}

void GamePersent::selectHell()
{
    QString city = Setting::getInstance()->getOutHellCity();

    if(city == HELL_LY){
        //寻找洛阳坐标
        ImgHelper::getGameMatchPoint(":/runshop/icon/hell_ly.jpg", qRectF);
        qDebug() << "位置" << city << qRectF->top();
    }else if(city == HELL_SZ){
        //去苏州
        ImgHelper::getGameMatchPoint(":/runshop/icon/hell_sz.jpg", qRectF);
        qDebug() << "位置" << city << qRectF->top();
    }else if(city == HELL_DL){
        //去大理
        ImgHelper::getGameMatchPoint(":/runshop/icon/hell_dl.jpg", qRectF);
        qDebug() << "位置" << city << qRectF->top();
    }else if(city == HELL_DHGZ){
        //去东河古镇
        ImgHelper::getGameMatchPoint(":/runshop/icon/hell_hdgz.jpg", qRectF);
        qDebug() << "位置" << city << qRectF->top();
    }
    QTimer::singleShot(TimeManager::getInstance()->getDelay(), this, [&](){
        //移动鼠标
        Win32Util::moveMouse( (qRectF->left() + qRectF->right())/ 2 , (qRectF->top() + qRectF->bottom()) /2);
        QTimer::singleShot(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset), this, [&](){
            Win32Util::mouseLeftClick();
            QTimer::singleShot(TimeManager::getInstance()->getDelay(5000), this, [&](){
                GameManager::getInstantce()->isCheckHell = true;
                GameManager::getInstantce()->roleLocation = ROLE_LOCATION_NORMAL;
            });
        });
    });
}

void GamePersent::addBlood(){
    if(getBlood() < BLOOD_ADD_VALUE){
        DeviceUtils::sendLog(widget,QString("血量低于%1%").arg(BLOOD_ADD_VALUE));
        if(!clickFuncKey(Setting::getInstance()->getBloodKey())){
            DeviceUtils::sendLog(widget, "未配置角色加血键");
            return;
        }
        DeviceUtils::sendLog(widget, "角色自动加血中...");
    }
}

void GamePersent::addMagic(){
    if(getMagic() < BLOOD_ADD_VALUE){
        DeviceUtils::sendLog(widget,QString("蓝量低于%1%").arg(BLOOD_ADD_VALUE));
        if(!clickFuncKey(Setting::getInstance()->getMagicKey())){
            DeviceUtils::sendLog(widget, "未配置角色加蓝键");
            return;
        }
        DeviceUtils::sendLog(widget, "角色自动加蓝中...");
    }
}

void GamePersent::addSpeed()
{
    if(!clickFuncKey(Setting::getInstance()->getSpeed())){
        DeviceUtils::sendLog(widget, "未配置角色加速键");
        return;
    }
    DeviceUtils::sendLog(widget, "角色释放加速技能...");
}

int GamePersent::getGameTime()
{
   QRect gameRect = GameManager::getInstantce()->gameRect;
    QRect shiChenRect = QRect(gameRect.right() - 260, 20, 100, 85);
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/1zi.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_ZI;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/2chou.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_CHOU;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/3yin.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_YIN;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/4mao.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_MAO;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/5chen.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_CHEN;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/6si.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_SI;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/7wu.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_WU;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/8wei.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_WEI;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/9shen.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_SHEN;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/10you.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_YOU;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/11xu.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_XU;
    }
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return 0;
    if(ImgHelper::getGameMatchPointAsb(":/time/icon/shichen/12hai.png",Scalar(17,136,255),Scalar(94,221,255),0.9f,shiChenRect)){
        return SHICHEN_HAI;
    }
    return GROUP2ERROR;
}

void GamePersent::clickXiaoWei(QString enName, int cityIndex){
    cityIndex = cityIndex -1;
    qDebug() << "enName" <<enName;
//    clickNPC(":/runshop/icon/runshop_xiaowei.png",Scalar(29, 209, 161), Scalar(31, 255, 255),true);
    QThread::msleep(1000);
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    QList<QSharedPointer<QRectF>> rectfs;

    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL || !ImgHelper::getGameMatchMultiPoint(":/runshop/icon/city.png",Scalar(14, 109, 83), Scalar(26, 171, 249), 0.7f, &rectfs, QRectF(0, 0 ,0.5f, 0.7f))){
        DeviceUtils::sendLog(widget, QString("未识别到你所在的帮派"));
        return;
    }
    DeviceUtils::sendLog(widget, QString("识别到该处有%1个帮派").arg(rectfs.size()));
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    if(cityIndex < 0 || cityIndex >= rectfs.size()){
        GameManager::getInstantce()->roleTask = ROLE_TASK_NORMAL;
        DeviceUtils::sendLog(widget, QString("城市索引设置错误"));
        return;
    }
    QSharedPointer<QRectF> rectf = rectfs.at(cityIndex);
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::moveMouse(rectf->right() + 10 , (rectf->top() + rectf->bottom()) /2 );
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->timeOffset));
    if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    Win32Util::mouseLeftClick();  //点击 帮派名称
    DeviceUtils::sendLog(widget,QString("进入第%1个帮派").arg(cityIndex + 1));
    while(!getMapString().contains(enName)){
        QThread::msleep(100);
        if(GameManager::getInstantce()->roleTask == ROLE_TASK_NORMAL) return;
    }
    QThread::msleep(TimeManager::getInstance()->getDelay(TimeManager::getInstance()->scene));
}


GamePersent::~GamePersent()
{
    if(qRectF != nullptr){
        delete qRectF;
        qRectF = nullptr;
    }
}

